package com.example.flight_ticket_app.controller;

import com.alibaba.fastjson.JSON;
import com.example.flight_ticket_app.bean.*;
import com.example.flight_ticket_app.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ServerController {

    @Autowired
    private ServerService serverService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    @CrossOrigin
    public Result login(@RequestBody User user){
        try{
            return serverService.login(user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 客机页面分页
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/getPlanePage")
    @CrossOrigin
    public Result getPlanePage(Integer pageSize, Integer pageNum){
        try{
            return serverService.getPlanePage(pageSize, pageNum);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 航班页面分页
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/getFlightPage")
    @CrossOrigin
    public Result getFlightPage(Integer pageSize, Integer pageNum){
        try{
            return serverService.getFlightPage(pageSize, pageNum);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    /**
     * 机票页面分页
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/getTicketPage")
    @CrossOrigin
    public Result getTicketPage(Integer pageSize, Integer pageNum){
        try{
            return serverService.getTicketPage(pageSize, pageNum);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 订单页面分页
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/getIndentPage")
    @CrossOrigin
    public Result getIndentPage(Integer pageSize, Integer pageNum){
        try{
            return serverService.getIndentPage(pageSize, pageNum);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 退票页面分页
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/getRefundPage")
    @CrossOrigin
    public Result getRefundPage(Integer pageSize, Integer pageNum){
        try{
            return serverService.getRefundPage(pageSize, pageNum);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 删除指定客机
     * @param planeId
     * @return
     */
    @DeleteMapping("/deletePlane")
    @CrossOrigin
    public Result deletePlane(Integer planeId){
        try{
            return serverService.deletePlane(planeId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    /**
     * 删除指定航班
     * @param flightId
     * @return
     */
    @DeleteMapping("/deleteFlight")
    @CrossOrigin
    public Result deleteFlight(String flightId){
        try{
            return serverService.deleteFlight(flightId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 删除指定订单
     * @param id
     * @return
     */
    @DeleteMapping("/deleteIndent")
    @CrossOrigin
    public Result deleteIndent(Integer id){
        try{
            return serverService.deleteIndent(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 删除指定退票信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteRefund")
    @CrossOrigin
    public Result deleteRefund(Integer id){
        try{
            return serverService.deleteRefund(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    /**
     * 新增客机
     * @param plane
     * @return
     */
    @PostMapping("/addPlane")
    @CrossOrigin
    public Result addPlane(@RequestBody Plane plane){
        try{
            return serverService.addPlane(plane);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 获取指定客机的信息
     * @param planeId
     * @return
     */
    @GetMapping("/getPlaneMessage")
    @CrossOrigin
    public Result getPlaneMessage(Integer planeId){
        try{
            return serverService.getPlaneMessage(planeId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 获取空闲状态客机列表
     * @return
     */
    @GetMapping("/getIdlePlane")
    @CrossOrigin
    public Result getIdlePlane(){
        try{
            return serverService.getIdlePlane();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 修改客机信息
     * @param plane
     * @return
     */
    @PostMapping("/modifyPlaneMessage")
    @CrossOrigin
    public Result modifyPlaneMessage(@RequestBody Plane plane){
        try{
            return serverService.modifyPlaneMessage(plane);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 修改登机口
     * @param ticketId
     * @return
     */
    @GetMapping("/modifyGate")
    @CrossOrigin
    public Result modifyGate(Integer ticketId, String gate){
        try{
            return serverService.modifyGate(ticketId, gate);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 新增航班
     * @param params
     * @return
     */
    @PostMapping("/addFlight")
    @CrossOrigin
    public Result addFlight(@RequestBody HashMap<String, Object> params){
        String json = null;
        try{
            json = JSON.toJSONString(params.get("flightMessage"));
            Flight flight = JSON.parseObject(json, Flight.class);
            json = JSON.toJSONString(params.get("gate"));
            String gate = JSON.parseObject(json, String.class);
            json = JSON.toJSONString(params.get("planeId"));
            Integer planeId = JSON.parseObject(json, Integer.class);
            return serverService.addFlight(flight, gate, planeId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 新增订单
     * @param params
     * @return
     */
    @PostMapping("/addIndent")
    @CrossOrigin
    public Result addIndent(@RequestBody HashMap<String, Object> params){
        String json = null;
        try{
            json = JSON.toJSONString(params.get("passengerMessage"));
            Passenger passenger = JSON.parseObject(json, Passenger.class);
            json = JSON.toJSONString(params.get("cabin"));
            Integer cabin = JSON.parseObject(json, Integer.class);
            json = JSON.toJSONString(params.get("flightId"));
            String flightId = JSON.parseObject(json, String.class);
           return serverService.addIndent(passenger, flightId, cabin);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 获取指定航班下所有乘客信息
     * @param flightId
     * @return
     */
    @GetMapping("/getFlightPassengers")
    @CrossOrigin
    public Result getFlightPassengers(String flightId){
        try{
            return serverService.getFlightPassengers(flightId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 根据乘客身份信息删除其订单
     * @param passengerId
     * @return
     */
    @DeleteMapping("/deleteIndentByPassengerId")
    @CrossOrigin
    public Result deleteIndentByPassengerId(String passengerId){
        try{
           return serverService.deleteIndentByPassengerId(passengerId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    /**
     * 购买经济舱机票
     * @param maps
     * @return
     */
    @PostMapping("/getEconomyClass")
    @CrossOrigin
    public Result getEconomyClass(@RequestBody Map<String, Object> maps){
        String json = null;
        try{
            json = JSON.toJSONString(maps.get("passenger"));
            Passenger passenger = JSON.parseObject(json, Passenger.class);
            json = JSON.toJSONString(maps.get("flightId"));
            String flightId = JSON.parseObject(json, String.class);
            return serverService.getEconomyClass(passenger, flightId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 购买商务舱机票
     * @param maps
     * @return
     */
    @PostMapping("/getBusinessClass")
    @CrossOrigin
    public Result getBusinessClass(@RequestBody Map<String, Object> maps){
        String json = null;
        try{
            json = JSON.toJSONString(maps.get("passenger"));
            Passenger passenger = JSON.parseObject(json, Passenger.class);
            json = JSON.toJSONString(maps.get("flightId"));
            String flightId = JSON.parseObject(json, String.class);
            return serverService.getBusinessClass(passenger, flightId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 查看航班信息
     * @param indentId
     * @return
     */
    @GetMapping("/getFlightMessage")
    @CrossOrigin
    public Result getFlightMessage(Integer indentId){
        try{
            return serverService.getFlightMessage(indentId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    /**
     * 获取指定航班
     * @param flightId
     * @return
     */
    @GetMapping("/getFlight")
    @CrossOrigin
    public Result getFlight(String flightId){
        try{
            return serverService.getFlight(flightId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

    @PostMapping("/modifyFlight")
    @CrossOrigin
    public Result modifyFlight(@RequestBody Flight flight){
        try{
            return serverService.modifyFlight(flight);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }


    /**
     * 查看订单信息
     * @param indentId
     * @return
     */
    @GetMapping("/getIndentMessage")
    @CrossOrigin
    public Result getIndentMessage(Integer indentId){
        try{
            return serverService.getIndentMessage(indentId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }

}
